package cn.zyq.ykb.controller.h5.common;

import cn.hutool.core.util.IdUtil;
import cn.zyq.ykb.system.common.BaseController;
import cn.zyq.ykb.system.config.consts.system.SystemConfig;
import cn.zyq.ykb.system.utils.FileInfo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;


import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 公共接口类
 * @Author pengdoudou
 * @Date 2023/4/20 14:43
 */
@Slf4j
@Api(tags = "公共接口类")
@RestController(value = "H5CommonController")
@RequestMapping(value = "/api")
public class CommonController extends BaseController {

    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/file/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("【请求开始】上传文件,请求参数,file:{}", file.getOriginalFilename());

        Map map = new HashMap();

        FileInfo fileInfo = new FileInfo();

        String originalFilename = file.getOriginalFilename();
        fileInfo.setOriginalFileName(originalFilename);
        // 后缀
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
        fileInfo.setExt(ext);
        fileInfo.setFileSize(file.getSize());
        // 文件名UUID生成
        String fileName = IdUtil.fastSimpleUUID();
        fileInfo.setFileName(fileName + "." + ext);
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        String folder = applicationHome.getDir().getParentFile()
                .getParentFile().getAbsolutePath()  + "/src/main/resources/static/images/";
        if (!SystemConfig.env.equals("dev")){
            folder = SystemConfig.fileBasePath;
        }

        File folderFile = new File( folder);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }

        String relativePath = folder + fileName + "." + ext;
        log.info("存储文件地址：" + relativePath);
        fileInfo.setRelativePath(relativePath);

        try {
            //获取item中的上传文件的输入流
            InputStream is = file.getInputStream();
            //创建一个文件输出流
            FileOutputStream fos = new FileOutputStream(relativePath);
            //创建一个缓冲区
            byte buffer[] = new byte[1024];
            //判断输入流中的数据是否已经读完的标识
            int length = 0;
            //循环将输入流读入到缓冲区当中，(len=in.read(buffer))>0就表示in里面还有数据
            while ((length = is.read(buffer)) > 0) {
                //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                fos.write(buffer, 0, length);
            }
            //关闭输入流
            is.close();
            //关闭输出流
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileInfo.setRelativePath("/images/"+ fileName + "." + ext);
        fileInfo.setUrl(fileInfo.getRelativePath());
        log.info("【请求结束】上传文件,响应结果:{}", JSONObject.toJSONString(fileInfo));
        return fileInfo;
    }


}
